package com.hunan.api.catv.mq.consumer;

import com.google.gson.JsonObject;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.collect.CollectBiz;
import com.hunan.api.catv.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CollectConsumer {
    @Autowired
    CollectBiz collectBiz;

    public static Logger log = LoggerFactory.getLogger(CollectConsumer.class);

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.collect_queue, durable = "true"), exchange = @Exchange(value = MqConstants.collect_exchange, durable = "true", type = "topic"), key = MqConstants.collect_routingkey))
    public void collect(String msg) {
        ServiceResult result = new ServiceResult();
        try {
            log.info("[mq收到收藏msg：{}]", msg);
            JsonObject returnData = GsonUtils.fromJson(msg, JsonObject.class);
            Integer memberId = returnData.get("memberId").getAsInt();
            Integer contentType = returnData.get("contentType").getAsInt();
            Integer collectType = returnData.get("collectType").getAsInt();
            String itemId = returnData.get("itemId").getAsString();
            if (collectType == 0) {
                result = collectBiz.delete(memberId, itemId, contentType);
            }
            if (collectType == 1) {
                result = collectBiz.add(memberId, itemId, contentType);
            }
        } catch (Exception e) {
            if (!result.getStatus()) {
                log.error("[Collect del Error:{}]", e);
            }
        }

    }
}
